package com.zib.vread.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.dragListView)
    DragListView dragListView;
    private Context context = this;
    private List<HashMap<String, Object>> totalList = new ArrayList<HashMap<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        for (int i = 0; i < 30; i++) {
            HashMap<String, Object> itemHashMap = new HashMap<String, Object>();
            itemHashMap.put("item_image", R.drawable.com_tencent_open_notice_msg_icon_big);
            itemHashMap.put("item_text", ""+ Integer.toString(i));
            totalList.add(itemHashMap);
        }


        final SimpleAdapter mSimpleAdapter = new SimpleAdapter(this, totalList,
                R.layout.item_listview, new String[]{"item_image", "item_text"},
                new int[]{R.id.item_image, R.id.item_text});

        dragListView.setAdapter(mSimpleAdapter);
        dragListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyToast.showToast(context,"success"+position,3);
            }
        });

        dragListView.setOnChangeListener(new DragListView.OnChanageListener() {
            @Override
            public void onChange(int from, int to) {
                HashMap<String, Object> temp = totalList.get(from);
                if (from < to) {
                    for (int i = from; i < to; i++) {
                        Collections.swap(totalList, i, i + 1);
                    }
                } else if (from > to) {
                    for (int i = from; i > to; i--) {
                        Collections.swap(totalList, i, i - 1);
                    }
                }
                totalList.set(to, temp);
                mSimpleAdapter.notifyDataSetChanged();
            }
        });

    }
}
